/*
 * Copyright (c) 2011 Auzingers
 * Created by Oui-IT
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.auzingers.docsorganizer.gui;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.EditText;

/** Utility class for simple dialogs. */
public final class DialogUtil {

    /** Hide constructor of utility class. */
    private DialogUtil() {
        //private constructor of utility class
    }

    /**
     * Interface for showInputDialog, implementing class performs an action with the user's input string.
     */
    interface IInputReceived {
        /**
         * Called when the user clicked ok in the input dialog.
         * @param input the input entered by the user
         */
        public void processInput(String input);
    }

    /**
     * Shows an input dialog.
     * @param ctx current context
     * @param title dialog title
     * @param message message to be shown
     * @param listener listener that is called if the user clicked onto ok
     */
    public static void showInputDialog(Context ctx, String title, String message, final IInputReceived listener) {
        final AlertDialog.Builder alert = new AlertDialog.Builder(ctx);
        alert.setTitle(title);
        alert.setMessage(message);
        final EditText input = new EditText(ctx);
        input.setSingleLine(true);
        alert.setView(input);
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                final String value = input.getText().toString();
                listener.processInput(value);
            }
        });
        alert.show();
    }

    /**
     * Shows a message dialog with the given title and message.
     * @param ctx current context
     * @param title title of the dialog
     * @param message message to show
     * @param okAction action to perform after the user pressed ok
     */
    public static void showMessageDialog(Context ctx, String title, String message, final Runnable okAction) {
        final AlertDialog.Builder alert = new AlertDialog.Builder(ctx);
        alert.setTitle(title);
        alert.setMessage(message);
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                if (okAction != null) {
                    okAction.run();
                }
            }
        });
        alert.show();
    }

    /**
     * Shows a question dialog with the given title and message.
     * @param ctx current context
     * @param title title of the dialog
     * @param message message to show
     * @param yesAction action to perform after the user pressed yes
     * @param noAction action to perform after the user pressed no
     */
    public static void showQuestionDialog(Context ctx, String title, String message, final Runnable yesAction,
            final Runnable noAction) {

        final AlertDialog.Builder alert = new AlertDialog.Builder(ctx);
        alert.setTitle(title);
        alert.setMessage(message);
        alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                if (yesAction != null) {
                    yesAction.run();
                }
            }
        });
        alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                if (noAction != null) {
                    noAction.run();
                }
            }
        });
        alert.show();
    }
}
